package com.wwt.blog.utils;

import com.alibaba.fastjson.JSON;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;


public class QiniuUtils {
    public static final String myUrl="r2d3kyocu.hn-bkt.clouddn.com";
    private static String accessKey="dhOOTjvJSqine6xFa75iqHBc7VFnB-ObGJ7C-Lkp";
    private static String accessSecretKey="_2x3-RfgfNWyn-qyV54F1unPMKaWV_zDLKF78JE8";

    public static  boolean upload(MultipartFile file,String filename){
        //构造一个带指定 Region 对象的配置类
        Configuration configuration = new Configuration(Region.huanan());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(configuration);
        String bucket = "wwtao";   //空间名
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        try {
            byte[] uploadBytes = file.getBytes();
            Auth auth = Auth.create(accessKey, accessSecretKey);
            String upToken = auth.uploadToken(bucket);
                Response response = uploadManager.put(uploadBytes, filename, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
                return true;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        return false;
    }
}
